<?php
namespace console\controllers;


use common\models\Active;
use common\models\ActiveAttr;
use common\models\Group;
use common\models\Order;
use common\service\ConstantService;
use common\service\RedisServer;
use yii\console\Controller;

class ActivityController extends Controller
{

    /**
     * php yii activity/activity-status
     * 活动过期自动暂停
     */
    public function actionActivityStatus()
    {
        $query=Active::find();
        $query->orWhere(['<=','end_time',time()]);
        $activity=$query->asArray()->all();
        if(empty($activity)){
            return true;
        }
        $activity_ids = array_column($activity,'id');
        $updata=Active::updateAll(['status'=>2],['id'=>$activity_ids]);

        $activity_attrs = ActiveAttr::find()->where(['active_id'=>$activity_ids])->asArray()->all();
        if(!empty($activity_attrs)){
            $redis = RedisServer::initialization();
            foreach ($activity_attrs as $val){
                $key = 'goods_'.$val['id'];
                $redis->delRedis($key);
            }
        }
        //默认正在拼单的团为已成团
        $groups = Group::find()->where(['active_id'=>$activity_ids,'status'=>2])->asArray()->all();
        if(empty($groups)){
            return true;
        }
        $group_ids = array_column($groups,'id');
        Group::updateAll(['status' => 1,'complete_time'=>time()],['id'=>$group_ids]);
        //将对应的订单变成待发货
        $re = Order::updateAll(['order_status'=>ConstantService::$order_status['WAIT_DELIVER']],['group_id'=>$group_ids,'order_status'=>ConstantService::$order_status['GROUPING']]);
        return true;
    }

}